// the Calendar module, comtain the .h .cpp and .ui

#ifndef CALENDAR_H
#define CALENDAR_H
#include<QObject>
#include <QtGui/QWidget>
#include <QDate>
#include <QTextCharFormat>
#include "ToListContent.h"
#include <QList>
#include <QTableWidget>
#include <QtXml/QDomDocument>
#include "global.h"

namespace Ui
{
   class Calendar;
}

class Calendar : public QWidget
{
    Q_OBJECT
public:

    Calendar(QWidget *parent = 0);

    int DlFromSever(); //downloading schedule from Sever

    int SaveToSever();  //uploading schedule to sever
    //for the main widget difine if the user have saved the changes
    void setChanged(bool value);
    bool getChanged();
    ~Calendar();

protected:

    //add the todolist Form dynamically
    void AddRecordForm();

    void DeleteRecordForm();

    // eveytime invoking the showToDoRecord, a record will be shown on the window
    //if one day have onemore records , invoking moretime is needed
    void showToDoRecord(int index, QString content);


    /**************************************************************
      // package Fucntions include add and modify remove
      //
     ***********************************************************************/
     void addToDoListToPackage(QDate const date,int const index, QString const content);
     void ModifyToDoListInPackage(QDate const date,int const index, QString const newContent);
     void RemoveToListFromPackage(QDate const date,int const index);


     /**************************************************************
       // other view effect
       //
      ***********************************************************************/
     void setDateTextFormat(QDate date, QTextCharFormat *Format);


     /**************************************************************
       // package to string and string to package functions
       //
      ***********************************************************************/
       QString PackageToString(ToDoListPackage *Package);
       ToDoListPackage* StringToPackage(QString str);


       /**************************************************************
         // data process, in order to transmission throung net
            the following functions is to transform the
         // package to byteArray and from byteArray to package
        ***********************************************************************/
       QByteArray PackageToByteArray(ToDoListPackage *package);
       ToDoListPackage ByteArrayToPackage(QByteArray byteArray);
       QDate StringToDate(QString date);


    /*********************************************************************/
     //toDolistTable
    void toDolistTable();
    //add row function is to add one row when user click add one record
    void addRow();
    //removeAll(),remove current todolist in a specific day
    void removeAll();
     /*********************************************************************/

signals:
    ///use this signal send your packege
    void sendPackageRequest(QByteArray package);
private slots:
    ///use this slot the receive package
    void processPackageResponse(MSGHeader header,QByteArray data);
    void on_calendarWidget_clicked(QDate date);
    void saveChanges();
   // setDateTextFormat ( const QDate & date, const QTextCharFormat & format );


   void on_refreshButton_clicked();

   void on_addButton_clicked();

   void on_deleteButton_clicked();

   void on_tableWidget_itemChanged(QTableWidgetItem* item);

   void on_saveButton_clicked();

private:

    bool changed;      //whether the schedule changed
    void refresh();
    QTextCharFormat *ToDoDateFormat; //define the format on the date which is in todolist
    Ui::Calendar *ui;
    ToDoListPackage *toDoListPackage;
    //save the change to a temp list. if user click save, then merge
    //the list to the toDoListPackage, otherwise clear it.
    QList<ChangedRecord> *changedRecord;
    QList<ToDoListForm> * toDoListForm;
    QList<QDate>* todoListDates;
};

#endif
